package cn.yx.common.i18n.service;

import cn.hutool.core.util.ArrayUtil;
import cn.yx.sys.remote.dto.SysI18nMessageRemoteDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>i18n消息服务类</p>
 *
 * @author Wgssmart
 */
@Component
@Slf4j
public class I18nMessageService {

    private final I18nMessageCacheService i18nMessageCacheService;

    public I18nMessageService(I18nMessageCacheService i18nMessageCacheService) {
        this.i18nMessageCacheService = i18nMessageCacheService;
    }

    /**
     * 调用远程接口查询国际化信息，不使用消息参数
     *
     * @param messageKey 消息Key
     * @return
     */
    public SysI18nMessageRemoteDto getSysI18nMessage(String messageKey) {
        return i18nMessageCacheService.getSysI18nMessage(messageKey);
    }

    /**
     * 调用远程接口查询国际化信息，使用消息参数
     *
     * @param messageKey    消息key
     * @param messageParams 消息参数
     * @return
     */
    public SysI18nMessageRemoteDto getSysI18nMessage(String messageKey, String[] messageParams) {
        SysI18nMessageRemoteDto sysI18nMessageRemoteDto = i18nMessageCacheService.getSysI18nMessage(messageKey);
        if (ArrayUtil.isNotEmpty(messageParams)) {
            String message = sysI18nMessageRemoteDto.getMessage();
            for (int i = 0, len = messageParams.length; i < len; i++) {
                message = message.replace("{" + i + "}", messageParams[i]);
            }
            sysI18nMessageRemoteDto.setMessage(message);
        }
        return sysI18nMessageRemoteDto;
    }

}
